//
//  BaseTableView.m
//  HuairouInsight
//
//  Created by xiangming on 14-5-4.
//  Copyright (c) 2014年 elitework. All rights reserved.
//

#import "BaseTableView.h"

@implementation BaseTableView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
         [self _initView];
    }
    return self;
}
//复写父类的法initWithFrame:(CGRect)frame style:(UITableViewStyle)style方法
#pragma mark - Override  initWithFrame: style: Method

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if(self!=nil){
        [self _initView];

    }
    return self;
}

- (void)awakeFromNib{
    [self _initView];
}

- (void)_initView
{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reloadData) name:kReloadWeiboTableNotification
                                               object:nil];
        //下拉刷新视图
        _refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f-self.bounds.size.height, self.bounds.size.width, self.bounds.size.height)];
        _refreshHeaderView.delegate = self;
        _refreshHeaderView.backgroundColor = COLOR_RGB(252, 252, 252);
        self.dataSource = self;
        self.delegate = self;
//        self.isNeedTorefresh = YES;
    
        //上拉刷新视图
        _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _moreButton.backgroundColor = [UIColor clearColor];
        _moreButton.frame = CGRectMake(0, 0, kWidth, 40);
        [_moreButton setTitle:@"下拉加载更多" forState:UIControlStateNormal];
        _moreButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_moreButton setTitleColor:GrayTextColor forState:UIControlStateNormal];
        [_moreButton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        activityView.tag = 2013;
        activityView.frame = CGRectMake(100, 10, 20, 20);
        [activityView stopAnimating];
        [_moreButton addSubview:activityView];
        self.tableFooterView = _moreButton;
    
}


//开始加载

- (void)_startLoadMore{
    [_moreButton setTitle:@"正在加载..." forState:UIControlStateNormal];
    _moreButton.enabled = NO;
    UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView*)[_moreButton viewWithTag:2013];
    [activityIndicator startAnimating];
}

//停止加载
- (void)_stopLoadMore{
    if(self.data.count > 0)
    {
        _moreButton.hidden = NO;
        [_moreButton setTitle:@"上拉加载更多..." forState:UIControlStateNormal];
        _moreButton.enabled = YES;
        UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView*)[_moreButton viewWithTag:2013];
        [activityIndicator stopAnimating];
        if (!self.isMore) {
            [_moreButton setTitle:@"加载完成" forState:UIControlStateNormal];
            _moreButton.enabled = NO;
            
        }
    }
    else{
        _moreButton.hidden = YES;
    }
}


#pragma mark - override reloadData

- (void)reloadData
{
    [super reloadData];
    [self _stopLoadMore];
}

#pragma mark - Actions
- (void)moreButtonAction:(UIButton *)button
{
    if ([self.eventsDelegate respondsToSelector:@selector(pullUp:)]) {
        [self.eventsDelegate pullUp:self];
        [self _startLoadMore];
    }
    
}

- (void)refreshData
{
    [_refreshHeaderView refreshLoading:self];
}
#pragma mark - Override setters

//重写需不需要刷新标识变量，如不需要刷新，则不添加上或下拉视图
- (void)setIsNeedTorefresh:(BOOL)isNeedTorefresh
{
    _isNeedTorefresh = isNeedTorefresh;
    if(_isNeedTorefresh){
        //下拉刷新视图初始化
        
        [self addSubview:_refreshHeaderView];
        
        [_refreshHeaderView refreshLastUpdatedDate];
    }
    else{
        if([_refreshHeaderView superview]){
            [_refreshHeaderView removeFromSuperview];
        }
    }
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.data.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.eventsDelegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
        [self.eventsDelegate basetableView:self didSelectRowAtIndexPath:indexPath];
    }
}

#pragma mark - 下拉相关的方法

- (void)reloadTableViewDataSource{
	_reloading = YES;
}

//停止加载，弹回下拉
- (void)doneLoadingTableViewData {
	_reloading = NO;
	[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self];
}


#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
	if(_isNeedTorefresh)
        [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
	if(_isNeedTorefresh)
        [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
	if(!self.isMore){
        return;
    }
    //y偏移量
    float offSet = scrollView.contentOffset.y;
    //滑动视图内容高度
    float contentHeight = scrollView.contentSize.height;
    float sub = contentHeight - offSet;
    if(scrollView.height-sub>30){
        [self _startLoadMore];
        if ([self.eventsDelegate respondsToSelector:@selector(pullUp:)]) {
            [self.eventsDelegate pullUp:self];
        }
    }
}

#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods
//下拉到一定距离，手指放开时调用
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
	//设置为 “正在加载” 状态
	[self reloadTableViewDataSource];
    
    if([self.eventsDelegate respondsToSelector:@selector(pullDown:)]) {
        [self.eventsDelegate pullDown:self];
    }
	
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
	
	return _reloading;
	
}

//取得下拉刷新的时间
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
	
	return [NSDate date];
	
}



@end
